package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Education;
import net.zjitc.service.IEducationService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-04-27 16:13:09
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "学历管理列表")
public class EducationController {
    @Reference
    private IEducationService educationService;
    ResponseResult result=new ResponseResult();

    @ApiOperation("分页获取学历信息")
    @GetMapping("education")
    public ResponseResult getEducations(@RequestParam(required = false,value ="querystring" ) String querystring,
                                      @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                      @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<Education> educations=educationService.getEducations(querystring,pagenum,pagesize);
        if (educations.getTotal()>0){
            result.setSuccess("获取学历信息成功",educations);
        }else {
            result.setError("获取学历信息失败");
        }
        return result;
    }
    @ApiOperation("获取所有学历信息")
    @GetMapping("educations")
    public ResponseResult getAllEducations(){
        List<Education> educations= educationService.getAllEducations();
        if (!educations.isEmpty()){
            result.setSuccess("获取学历信息成功",educations);
        }else {
            result.setError("获取学历信息失败");
        }
        return result;
    }

    @ApiOperation("根据id查找学历信息")
    @GetMapping("education/{id}")
    public ResponseResult getEducationById(@PathVariable("id") Integer id){
        Education education = educationService.getById(id);
        if (education!=null){
            result.setSuccess("获取学历信息成功",education);
        }else {
            result.setError("获取学历信息失败");
        }
        return result;
    }
    @ApiOperation("添加学历信息")
    @PostMapping("education")
    public ResponseResult AddEducation(@RequestBody Education education){
        Date time=new Date();
        education.setAddtime(time);
        boolean save = educationService.save(education);
        if (save){
            result.setCreateSuccess("添加学历信息成功");
        }else {
            result.setError("添加学历信息失败");
        }
        return result;
    }
    @ApiOperation("修改学历信息")
    @PutMapping("education/{id}")
    public ResponseResult UpdateEducation(@RequestBody Education education,@PathVariable("id") int id){
        education.setId(id);
        boolean update = educationService.updateById(education);
        if (update){
            result.setSuccess("修改学历信息成功");
        }else {
            result.setError("修改学历信息失败");
        }
        return result;
    }
    @ApiOperation("删除学历信息")
    @DeleteMapping("education/{id}")
    public ResponseResult DeleteEducation(@PathVariable("id") int id){

        int delete= educationService.deleteById(id);
        if (delete!=0){
            result.setSuccess("删除学历信息成功");
        }else {
            result.setError("删除学历信息失败");
        }
        return result;
    }

}

